package org.dromara.run.controller;

import java.util.Date;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.basic.domain.bo.PlaceBo;
import org.dromara.basic.domain.vo.PlaceVo;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.validate.OtherGroup;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.run.domain.bo.RunListBo;
import org.dromara.run.service.IRunListService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.run.domain.vo.ApplicationVo;
import org.dromara.run.domain.bo.ApplicationBo;
import org.dromara.run.service.IApplicationService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 免跑申请表
 *
 * @author Lion Li
 * @date 2024-08-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/run/application")
public class ApplicationController extends BaseController {

    private final IApplicationService applicationService;
    private final IRunListService runListService;
    /**
     * 查询免跑申请表列表
     */
    @SaCheckPermission("run:application:list")
    @GetMapping("/list")
    public TableDataInfo<ApplicationVo> list(ApplicationBo bo, PageQuery pageQuery) {
        return applicationService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询跑区列表详细信息
     *
     * @param bo 查询主体
     */
    @SaCheckPermission("run:application:query")
    @GetMapping("/infoList")
    public R<List<ApplicationVo>> getInfoList(ApplicationBo bo) {
        return R.ok(applicationService.queryList(bo));
    }
    /**
     * 导出免跑申请表列表
     */
    @SaCheckPermission("run:application:export")
    @Log(title = "免跑申请表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ApplicationBo bo, HttpServletResponse response) {
        List<ApplicationVo> list = applicationService.queryList(bo);
        ExcelUtil.exportExcel(list, "免跑申请表", ApplicationVo.class, response);
    }

    /**
     * 获取免跑申请表详细信息
     *
     * @param applicationId 主键
     */
    @SaCheckPermission("run:application:query")
    @GetMapping("/{applicationId}")
    public R<ApplicationVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long applicationId) {
        return R.ok(applicationService.queryById(applicationId));
    }

    /**
     * 新增免跑申请表
     */
    @SaCheckPermission("run:application:add")
    @Log(title = "免跑申请表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ApplicationBo bo) {
        return toAjax(applicationService.insertByBo(bo));
    }

    /**
     * 修改免跑申请表
     */
    @SaCheckPermission("run:application:edit")
    @Log(title = "免跑申请表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ApplicationBo bo) {
        return toAjax(applicationService.updateByBo(bo));
    }

    /**
     * 删除免跑申请表
     *
     * @param applicationIds 主键串
     */
    @SaCheckPermission("run:application:remove")
    @Log(title = "免跑申请表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{applicationIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] applicationIds) {
        return toAjax(applicationService.deleteWithValidByIds(List.of(applicationIds), true));
    }

    /**
     * 审批
     */
    @SaCheckPermission("run:application:edit")
    @Log(title = "免跑申请表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/approve")
    public R<Void> approve(@Validated(OtherGroup.class) @RequestBody ApplicationBo bo) {
        bo.setApproveDate(new Date());
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setApprover(loginUser.getUserId().toString());
        Boolean scheduleResult = applicationService.updateByBo(bo);
        if(!scheduleResult ){
            return R.fail("审批失败");
        }
        if("1".equals(bo.getApproveStatus())){
            Boolean aBoolean = runListService.updateByMemberId(bo);
            if(!aBoolean ){
                return R.fail("审批失败");
            }

        }
        return R.ok("审批成功");
    }
}
